﻿/*
*	城市三级地址联动
*/
function Dsy(){
	this.Items = {};
}
Dsy.prototype.add = function(id,iArray){
	this.Items[id] = iArray;
}
Dsy.prototype.Exists = function(id){
	if(typeof(this.Items[id]) == "undefined") return false;
	return true;
}

function change(v){
	var str="0";
	for(i=0;i<v;i++){
		str+=("_"+(document.getElementById(s[i]).selectedIndex-1));
	};
	var ss=document.getElementById(s[v]);
	with(ss){
		length = 0;
		options[0]=new Option(opt0[v],opt0[v]);

		if(v && document.getElementById(s[v-1]).selectedIndex>0 || !v){
			if(dsy.Exists(str)){
				ar = dsy.Items[str];
				for(i=0;i<ar.length;i++){
					options[length]=new Option(ar[i],ar[i]);
				}//end for
				if(v){ options[0].selected = true; }
			}
		}//end if v
		if(++v<s.length){change(v);}
	}//End with
}

function change_a(v){
	var str="0";
	for(i=0;i<v;i++){
		str+=("_"+(document.getElementById(a[i]).selectedIndex-1));
	};
	var ss=document.getElementById(a[v]);
	with(ss){
		length = 0;
		options[0]=new Option(opt0[v],opt0[v]);

		if(v && document.getElementById(a[v-1]).selectedIndex>0 || !v){
			if(dsy.Exists(str)){
				ar = dsy.Items[str];
				for(i=0;i<ar.length;i++){
					options[length]=new Option(ar[i],ar[i]);
				}//end for
				if(v){ options[0].selected = true; }
			}
		}//end if v
		if(++v<a.length){change(v);}
	}//End with
}

function change_b(v){
	var str="0";
	for(i=0;i<v;i++){
		str+=("_"+(document.getElementById(b[i]).selectedIndex-1));
	};
	var ss=document.getElementById(b[v]);
	with(ss){
		length = 0;
		options[0]=new Option(opt0[v],opt0[v]);

		if(v && document.getElementById(b[v-1]).selectedIndex>0 || !v){
			if(dsy.Exists(str)){
				ar = dsy.Items[str];
				for(i=0;i<ar.length;i++){
					options[length]=new Option(ar[i],ar[i]);
				}//end for
				if(v){ options[0].selected = true; }
			}
		}//end if v
		if(++v<b.length){change(v);}
	}//End with
}


var dsy = new Dsy();


dsy.add("0",["亳州市"]);
dsy.add("0_0",["谯城区","涡阳县","蒙城县","利辛县"]);
dsy.add("0_0_0",["北一环路", "古泉路", "汤陵路", "和平路", "人民路", "利辛路", "交通路", "文帝路", "光明路", "芍花大道", "药都大道", "长江路", "古井路", "南一环", "老子路", "子建路", "庄子路", "经一路", "工业路", "研发路", "振谯路", "科技路", "西一环", "规划路", "仙翁路", "漆园路", "药王路", "桐乡路", "涡阳路", "陵西路", "菊花路", "芍香路", "谯陵路", "汤王大道", "希夷大道", "三曹路", "魏武大道", "建安路", "京港大道", "世纪大道", "迎宾大道", "章华路", "牡丹路", "中宫路", "楚哲路", "新华路", "新华北路", "古井镇","大杨镇", "古城镇", "芦庙镇", "华佗镇", "魏岗镇", "牛集镇", "颜集镇", "五马镇", "十八里镇", "十九里镇", "谯东镇", "沙土镇", "观堂镇", "城父镇", "十河镇", "双沟镇", "淝河镇", "龙扬镇", "立德镇", "张店乡", "赵桥乡", "安溜镇", "大寺镇", "三官镇"]);
dsy.add("0_0_1",["城关街道", "城东街道", "城西街道", "涡北街道", "西阳镇", "涡南镇", "楚店镇", "高公镇", "高炉镇", "曹市镇", "青疃镇", "石弓镇", "龙山镇", "义门镇", "新兴镇", "临湖镇", "丹城镇", "马店集镇", "花沟镇", "店集镇", "陈大镇", "牌坊镇", "公吉寺镇", "标里镇", "单集林场"]);
dsy.add("0_0_2",["城关街道", "庄周街道", "漆园街道", "小辛集乡", "许疃镇", "岳坊镇", "王集乡", "双涧镇", "乐土镇", "马集镇", "板桥集镇", "小涧镇", "篱笆镇", "立仓镇", "楚村镇", "坛城镇", "白杨林场", "范集镇", "三义镇"]);
dsy.add("0_0_3",["王市镇", "城关镇", "阚疃镇", "张村镇", "江集镇", "旧城镇", "西潘楼镇", "孙集镇", "汝集镇", "巩店镇", "王人镇", "永兴镇", "马店孜镇", "大李集镇", "胡集镇", "展沟镇", "程家集镇", "中疃镇", "望疃镇", "刘家集乡", "纪王场乡", "孙庙乡", "新张集乡"]);

var s=["s_city","s_county","s_town"];//三个select的name
var opt0 = ["地市","区县","乡镇/街道"];//初始值

function _init_area(){  //初始化函数
	for(i=0;i<s.length-1;i++){
	  document.getElementById(s[i]).onchange=new Function("change("+(i+1)+")");
	}
	change(0);
}

var a=["a_city","a_county","a_town"];//三个select的name

function _init_area_a(){  //初始化函数
	for(i=0;i<a.length-1;i++){
	  document.getElementById(a[i]).onchange=new Function("change_a("+(i+1)+")");
	}
	change_a(0);
}

var b=["b_city","b_county","b_town"];//三个select的name

function _init_area_b(){  //初始化函数
	for(i=0;i<b.length-1;i++){
	  document.getElementById(b[i]).onchange=new Function("change_b("+(i+1)+")");
	}
	change_b(0);
}

// 利用AJAX技术实现数据提交与获取响应

function _init_sitename_a(){  // town选择按钮的响应函数，为sitename提供选项值
    document.getElementById("a_town").onchange=function(){
        var xmlHttp = new XMLHttpRequest();
        var city = document.getElementById("a_city").value;
        var county = document.getElementById("a_county").value;
        var town = document.getElementById("a_town").value;

        var s_type = "a_sitename";

        xmlHttp.open("post", "relevanceajax.jsp?s_type=" + encodeURI(s_type) + "&s_city=" + encodeURI(city) + "&s_county=" + encodeURI(county) + "&s_town=" + encodeURI(town));

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                document.getElementById("id_a").innerHTML = xmlHttp.responseText;
            }
        }

        xmlHttp.send(null);

    }
}

function _init_sitename_b(){  // town选择按钮的响应函数，为sitename提供选项值
    document.getElementById("b_town").onchange=function(){
        var xmlHttp = new XMLHttpRequest();
        var city = document.getElementById("b_city").value;
        var county = document.getElementById("b_county").value;
        var town = document.getElementById("b_town").value;

        var s_type = "b_sitename";

        xmlHttp.open("post", "relevanceajax.jsp?s_type=" + encodeURI(s_type) + "&s_city=" + encodeURI(city) + "&s_county=" + encodeURI(county) + "&s_town=" + encodeURI(town));

        xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                document.getElementById("id_b").innerHTML = xmlHttp.responseText;
            }
        }

        xmlHttp.send(null);

    }
}






